home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Milan_1991 / Devcon91.1 / Libraries / GadTools / gadget2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-01  |  12.8 KB  |  644 lines

  1. /*
  2. ** gadget2.c:  Font-sensitive GadTools gadgets.
  3. **
  4. ** (C) Copyright 1990, Commodore-Amiga, Inc.
  5. **
  6. ** Executables based on this information may be used in software
  7. ** for Commodore Amiga computers.  All other rights reserved.
  8. ** This information is provided "as is"; no warranties are made.  All
  9. ** use is at your own risk. No liability or responsibility is assumed.
  10. */
  11.  
  12. #include <exec/types.h>
  13. #include <intuition/intuition.h>
  14. #include <intuition/gadgetclass.h>
  15. #include <libraries/gadtools.h>
  16.  
  17. #include <clib/exec_protos.h>
  18. #include <clib/graphics_protos.h>
  19. #include <clib/intuition_protos.h>
  20. #include <clib/diskfont_protos.h>
  21. #include <clib/gadtools_protos.h>
  22.  
  23. #include <string.h>
  24.  
  25. void printf(STRPTR,...);
  26. int stcd_l(char *, long *);
  27. void exit(int);
  28.  
  29. extern struct Library *SysBase;
  30.  
  31. /*------------------------------------------------------------------------*/
  32.  
  33. BOOL check1 = TRUE;
  34. BOOL check2 = FALSE;
  35. STRPTR settinga = "Bananas";
  36. STRPTR settingb = "Lemons";
  37.  
  38. STRPTR LeftLabels[] =
  39. {
  40.     "Button One",
  41.     "Button Two",
  42.     "Button Three",
  43.     "Button Four",
  44. };
  45.  
  46. STRPTR RightLabels[] =
  47. {
  48.     "Setting A:",
  49.     "Setting B:",
  50.     "Check 1:",
  51.     "Check 2:",
  52. };
  53.  
  54. UWORD WinWidth, WinHeight;
  55.  
  56. /*------------------------------------------------------------------------*/
  57.  
  58. void main(int, char *[]);
  59. struct Window *initWindow(struct Screen *, struct TextAttr *);
  60. void uninitWindow(struct Window *);
  61. void bail_out(int);
  62. struct Gadget *CreateAllGadgets(struct Gadget **, void *,
  63.     struct Screen *, struct TextAttr *);
  64. BOOL HandleEvent(struct Window *);
  65. void Hit_Button1(struct Window *, struct Gadget *);
  66. void Hit_Button2(struct Window *, struct Gadget *);
  67. void Hit_Button3(struct Window *, struct Gadget *);
  68. void Hit_Button4(struct Window *, struct Gadget *);
  69. void Hit_Check1(struct Window *, struct Gadget *);
  70. void Hit_Check2(struct Window *, struct Gadget *);
  71.  
  72. /*------------------------------------------------------------------------*/
  73.  
  74. /*  These are all the things to be allocated/opened, and later
  75.     freed/closed: */
  76.  
  77. struct GfxBase *GfxBase = NULL;
  78. struct IntuitionBase *IntuitionBase = NULL;
  79. struct Library *GadToolsBase = NULL;
  80. struct Library *DiskfontBase = NULL;
  81. struct Screen *mysc = NULL;
  82. struct Window *win = NULL;
  83. struct TextFont *customfont = NULL;
  84. struct Gadget *glist = NULL;
  85. void *vi = NULL;
  86.  
  87. /*------------------------------------------------------------------------*/
  88.  
  89. struct TextAttr customtattr;
  90. struct TextAttr *tattr;
  91.  
  92. /*------------------------------------------------------------------------*/
  93.  
  94. void main(argc, argv)
  95.  
  96. int argc;
  97. char *argv[];
  98.  
  99. {
  100.     /*  Open all libraries: */
  101.  
  102.     if (argc == 2)
  103.     {
  104.     printf("Usage:\n\tgadget2\nor\n\tgadget2 fontname.font fontsize\n");
  105.     printf("Example:\n\tgadget2 courier.font 15\n");
  106.     bail_out(0);
  107.     }
  108.  
  109.     if (!(GfxBase = (struct GfxBase *)
  110.     OpenLibrary("graphics.library", 36L)))
  111.     bail_out(20);
  112.  
  113.     if (!(IntuitionBase = (struct IntuitionBase *)
  114.     OpenLibrary("intuition.library", 36L)))
  115.     bail_out(20);
  116.  
  117.     if (!(GadToolsBase = OpenLibrary("gadtools.library", 36L)))
  118.     bail_out(20);
  119.  
  120.     if (!(DiskfontBase = OpenLibrary("diskfont.library", 36L)))
  121.     bail_out(20);
  122.  
  123.     if (!(mysc = LockPubScreen(NULL)))
  124.     bail_out(20);
  125.  
  126.     customtattr.ta_Style = 0;
  127.     customtattr.ta_Flags = 0;
  128.     if (argc < 3)
  129.     {
  130.     /*  Default to screen's font */
  131.     tattr = mysc->Font;
  132.     }
  133.     else
  134.     {
  135.     LONG longval;
  136.  
  137.     /*  Attempt to use the font specified on the command line: */
  138.     customtattr.ta_Name = argv[1];
  139.     /*  Convert decimal size to long */
  140.     stcd_l(argv[2], &longval);
  141.     customtattr.ta_YSize = longval;
  142.     tattr = &customtattr;
  143.     if (!(customfont = OpenDiskFont(tattr)))
  144.     {
  145.         printf("Could not open font %s %ld\n", customtattr.ta_Name,
  146.         customtattr.ta_YSize);
  147.         bail_out(20);
  148.     }
  149.     }
  150.  
  151.     win = initWindow(mysc, tattr);
  152.  
  153.     if (win)
  154.     {
  155.     while (!HandleEvent(win))
  156.         ;
  157.     bail_out(0);
  158.     }
  159.     else
  160.     bail_out(20);
  161. }
  162.  
  163. /*------------------------------------------------------------------------*/
  164.  
  165. /*/ initWindow()
  166.  *
  167.  *  Open a window, allocate and add all the gadgets.
  168.  *
  169.  */
  170.  
  171. struct Window *initWindow(screen, tattr)
  172.  
  173. struct Screen *screen;
  174. struct TextAttr *tattr;
  175.  
  176. {
  177.     BOOL ok;
  178.     struct Window *win = NULL;
  179.  
  180.     ok = ((vi = GetVisualInfo(screen,
  181.     TAG_DONE)) != NULL);
  182.  
  183.     if (ok)
  184.     {
  185.     ok = (CreateAllGadgets(&glist, vi, screen, tattr) != NULL);
  186.     }
  187.  
  188.     if (ok)
  189.     {
  190.     /*  Open the window: */
  191.     if (!(win = OpenWindowTags(NULL,
  192.     WA_InnerWidth, WinWidth,
  193.     WA_Height, WinHeight,
  194.     WA_AutoAdjust, TRUE,
  195.     WA_PubScreen, screen,
  196.     WA_Activate, TRUE,
  197.     WA_DragBar, TRUE,
  198.     WA_DepthGadget, TRUE,
  199.     WA_CloseGadget, TRUE,
  200.     WA_SizeGadget, TRUE,
  201.     WA_SimpleRefresh, TRUE,
  202.  
  203.     WA_IDCMP, CLOSEWINDOW | REFRESHWINDOW | BUTTONIDCMP | CHECKBOXIDCMP,
  204.  
  205.     WA_MinWidth, 50,
  206.     WA_MinHeight, 50,
  207.     WA_Title, "GadTools Font-Sensitive Gadgets",
  208.  
  209.     TAG_DONE)))
  210.     bail_out(20);
  211.     }
  212.  
  213.     if (ok)
  214.     {
  215.     /*  Add gadgets, refresh them, and special-refresh them.
  216.         GT_RefreshWindow() does the refreshing that RefreshGList()
  217.         will take care of when we switch over to a custom-gadget
  218.         implementation: */
  219.     AddGList(win, glist, ((UWORD) -1), ((UWORD) -1), NULL);
  220.     RefreshGList(glist, win, NULL, ((UWORD) -1));
  221.     GT_RefreshWindow(win, NULL);
  222.     return(win);
  223.     }
  224.     else
  225.     {
  226.     uninitWindow(win);
  227.     win = NULL;
  228.     return(NULL);
  229.     }    
  230. }
  231.  
  232. /*------------------------------------------------------------------------*/
  233.  
  234. /*/ uninitWindow()
  235.  *
  236.  * Take down a window and free stuff associated with it.
  237.  * It's ok to call this with a NULL pointer.
  238.  *
  239.  */
  240.  
  241. void uninitWindow(win)
  242.  
  243. struct Window *win;
  244.  
  245. {
  246.     if (win)
  247.     {
  248.     CloseWindow(win);
  249.     }
  250.     if (glist)
  251.     {
  252.     FreeGadgets(glist);
  253.     glist = NULL;
  254.     }
  255.     if (vi)
  256.     {
  257.     FreeVisualInfo(vi);
  258.     vi = NULL;
  259.     }
  260. }
  261.  
  262.  
  263. /*------------------------------------------------------------------------*/
  264.  
  265. /*/ bail_out()
  266.  *
  267.  * Function to close down or free any opened or allocated stuff, and then
  268.  * exit.
  269.  *
  270.  */
  271.  
  272. void bail_out(code)
  273.  
  274. int code;
  275.  
  276. {
  277.  
  278.     uninitWindow(win);
  279.     win = NULL;
  280.  
  281.     if (customfont)
  282.     {
  283.     CloseFont(customfont);
  284.     }
  285.  
  286.     if (mysc)
  287.     {
  288.     UnlockPubScreen(NULL, mysc);
  289.     }
  290.  
  291.     if (GadToolsBase)
  292.     {
  293.     CloseLibrary(GadToolsBase);
  294.     }
  295.  
  296.     if (DiskfontBase)
  297.     {
  298.     CloseLibrary(DiskfontBase);
  299.     }
  300.  
  301.     if (IntuitionBase)
  302.     {
  303.     CloseLibrary(IntuitionBase);
  304.     }
  305.  
  306.     if (GfxBase)
  307.     {
  308.     CloseLibrary(GfxBase);
  309.     }
  310.  
  311.     exit(code);
  312. }
  313.  
  314.  
  315. /*------------------------------------------------------------------------*/
  316.  
  317. /*/ CreateAllGadgets()
  318.  *
  319.  * Here is where all the initialization and creation of toolkit gadgets
  320.  * take place.  This function requires a pointer to a NULL-initialized
  321.  * gadget list pointer.  It returns a pointer to the last created gadget,
  322.  * which can be checked for success/failure.
  323.  *
  324.  */
  325.  
  326. struct Gadget *CreateAllGadgets(glistptr, vi, screen, tattr)
  327.  
  328. struct Gadget **glistptr;
  329. void *vi;
  330. struct Screen *screen;
  331. struct TextAttr *tattr;
  332.  
  333. {
  334.     struct NewGadget ng;
  335.     struct Gadget *gad;
  336.     WORD row1, row2, row3, row4;
  337.     WORD LeftWidth = 0;
  338.     WORD RightWidth = 0;
  339.     WORD FarRightWidth = 0;
  340.     WORD width;
  341.     struct RastPort TextRP;
  342.     WORD i;
  343.     UWORD topborder;
  344.     struct TextFont *font;
  345.  
  346.     topborder = screen->WBorTop + (screen->Font->ta_YSize + 1);
  347.  
  348.  
  349.     if (!(font = OpenFont(tattr)))
  350.     {
  351.     return(NULL);
  352.     }
  353.  
  354.     InitRastPort(&TextRP);
  355.     SetFont(&TextRP, font);
  356.  
  357.     /*  Need to know widest label in left and right columns */
  358.     for (i = 0; i < 3; i++)
  359.     {
  360.     if ( (width = TextLength(&TextRP, LeftLabels[i],
  361.         (WORD)strlen(LeftLabels[i]))) > LeftWidth)
  362.     {
  363.         LeftWidth = width;
  364.     }
  365.     if ( (width = TextLength(&TextRP, RightLabels[i],
  366.         (WORD)strlen(RightLabels[i]))) > RightWidth)
  367.     {
  368.         RightWidth = width;
  369.     }
  370.     }
  371.  
  372.     FarRightWidth = TextLength(&TextRP, settinga, strlen(settinga));
  373.     i = TextLength(&TextRP, settingb, strlen(settingb));
  374.     if (i > FarRightWidth)
  375.     {
  376.     FarRightWidth = i;
  377.     }
  378.  
  379.     /*  We obligingly perform the following operation, required of
  380.     any program that uses the toolkit.  It gives the toolkit a
  381.     place to stuff context data: */
  382.     gad = CreateContext(glistptr);
  383.  
  384.     /*  Fill out a NewGadget structure for each gadget we want
  385.     to create: */
  386.  
  387.     ng.ng_LeftEdge = 10;
  388.     row1 = ng.ng_TopEdge = 6+topborder;
  389.     ng.ng_Width = LeftWidth+16;
  390.     ng.ng_Height = font->tf_YSize+4;
  391.     ng.ng_GadgetText = LeftLabels[0];
  392.     ng.ng_TextAttr = tattr;
  393.     ng.ng_Flags = 0;
  394.     ng.ng_UserData = (APTR) Hit_Button1;
  395.     ng.ng_VisualInfo = vi;
  396.     gad = CreateGadget(BUTTON_KIND, gad, &ng,
  397.     TAG_DONE);
  398.  
  399.     if (gad)
  400.     {
  401.     row2 = ng.ng_TopEdge += gad->Height + 4;
  402.     }
  403.     ng.ng_GadgetText = LeftLabels[1];
  404.     ng.ng_UserData = (APTR) Hit_Button2;
  405.     gad = CreateGadget(BUTTON_KIND, gad, &ng,
  406.     TAG_DONE);
  407.  
  408.     if (gad)
  409.     {
  410.     row3 = ng.ng_TopEdge += gad->Height + 4;
  411.     }
  412.     ng.ng_GadgetText = LeftLabels[2];
  413.     ng.ng_UserData = (APTR) Hit_Button3;
  414.     gad = CreateGadget(BUTTON_KIND, gad, &ng,
  415.     TAG_DONE);
  416.  
  417.     if (gad)
  418.     {
  419.     row4 = ng.ng_TopEdge += gad->Height + 4;
  420.     }
  421.     ng.ng_GadgetText = LeftLabels[3];
  422.     ng.ng_UserData = (APTR) Hit_Button4;
  423.     gad = CreateGadget(BUTTON_KIND, gad, &ng,
  424.     TAG_DONE);
  425.  
  426.     if (gad)
  427.     {
  428.     WinHeight = ng.ng_TopEdge += gad->Height + 8;
  429.     }
  430.  
  431.     ng.ng_GadgetText = RightLabels[0];
  432.     ng.ng_UserData = NULL;
  433.     /*  The text in a Text gadget will be two pixels beneath a button
  434.     gadget's text. */
  435.     ng.ng_TopEdge = row1+2;
  436.     ng.ng_LeftEdge += ng.ng_Width + 26 + RightWidth;
  437.     ng.ng_Height = font->tf_YSize;
  438.     gad = CreateGadget(TEXT_KIND, gad, &ng,
  439.     GTTX_Text, settinga,
  440.     TAG_DONE);
  441.  
  442.     if (gad)
  443.     {
  444.     WinWidth = ng.ng_LeftEdge + FarRightWidth + 2;
  445.     }
  446.  
  447.     ng.ng_GadgetText = RightLabels[1];
  448.     ng.ng_TopEdge = row2+2;
  449.     gad = CreateGadget(TEXT_KIND, gad, &ng,
  450.     GTTX_Text, settingb,
  451.     TAG_DONE);
  452.  
  453.     ng.ng_GadgetText = RightLabels[2];
  454.     ng.ng_TopEdge = row3;
  455.     ng.ng_UserData = (APTR) Hit_Check1;
  456.     gad = CreateGadget(CHECKBOX_KIND, gad, &ng,
  457.     GTCB_Checked, check1,
  458.     TAG_DONE);
  459.  
  460.     ng.ng_GadgetText = RightLabels[3];
  461.     ng.ng_TopEdge = row4;
  462.     ng.ng_UserData = (APTR) Hit_Check2;
  463.     gad = CreateGadget(CHECKBOX_KIND, gad, &ng,
  464.     GTCB_Checked, check2,
  465.     TAG_DONE);
  466.  
  467.     CloseFont(font);
  468.     return(gad);
  469. }
  470.  
  471.  
  472. /*------------------------------------------------------------------------*/
  473.  
  474. /*/ HandleEvent()
  475.  *
  476.  * Handle an Intuition event.  Returns TRUE if a terminating
  477.  * event was found.
  478.  *
  479.  */
  480.  
  481. BOOL HandleEvent(win)
  482.  
  483. struct Window *win;
  484.  
  485. {
  486.     BOOL terminated = FALSE;
  487.     void (*fptr)(struct Window *, struct Gadget *);
  488.     struct IntuiMessage *imsg;
  489.     ULONG imsgClass;
  490.     struct Gadget *gad;
  491.  
  492.     Wait (1 << win->UserPort->mp_SigBit);
  493.     /*  GT_GetIMsg() returns a cooked-up IntuiMessage with
  494.     more friendly information for complex gadget classes.  Use
  495.     it wherever you get IntuiMessages: */
  496.     while (imsg = GT_GetIMsg(win->UserPort))
  497.     {
  498.     imsgClass = imsg->Class;
  499.     /*  Presuming a gadget, of course, but no harm... */
  500.     gad = (struct Gadget *)imsg->IAddress;
  501.     GT_ReplyIMsg(imsg);
  502.     switch (imsgClass)
  503.     {
  504.         case GADGETUP:
  505.         if (fptr = (void (*)(struct Window *, struct Gadget *))gad->UserData)
  506.         {
  507.             (*fptr)(win, gad);
  508.         }
  509.         break;
  510.  
  511.         case CLOSEWINDOW:
  512.         terminated = TRUE;
  513.         break;
  514.  
  515.         case REFRESHWINDOW:
  516.         /*  You must use GT_BeginRefresh() where you would
  517.             normally have your first BeginRefresh() */
  518.         GT_BeginRefresh(win);
  519.         GT_EndRefresh(win, TRUE);
  520.         break;
  521.     }
  522.     }
  523.     return(terminated);
  524. }
  525.  
  526.  
  527. /*------------------------------------------------------------------------*/
  528.  
  529. /*/ Hit_Button1()
  530.  *
  531.  * Button1 gadget handler.
  532.  *
  533.  */
  534.  
  535. void Hit_Button1(win, gad)
  536.  
  537. struct Window *win;
  538. struct Gadget *gad;
  539.  
  540. {
  541.     printf("Clicked on Button1\n");
  542. }
  543.  
  544.  
  545. /*------------------------------------------------------------------------*/
  546.  
  547. /*/ Hit_Button2()
  548.  *
  549.  * Button2 gadget handler.
  550.  *
  551.  */
  552.  
  553. void Hit_Button2(win, gad)
  554.  
  555. struct Window *win;
  556. struct Gadget *gad;
  557.  
  558. {
  559.     printf("Clicked on Button2\n");
  560. }
  561.  
  562.  
  563. /*------------------------------------------------------------------------*/
  564.  
  565. /*/ Hit_Button3()
  566.  *
  567.  * Button3 gadget handler.
  568.  *
  569.  */
  570.  
  571. void Hit_Button3(win, gad)
  572.  
  573. struct Window *win;
  574. struct Gadget *gad;
  575.  
  576. {
  577.     printf("Clicked on Button3\n");
  578. }
  579.  
  580.  
  581. /*------------------------------------------------------------------------*/
  582.  
  583. /*/ Hit_Button4()
  584.  *
  585.  * Button4 gadget handler.
  586.  *
  587.  */
  588.  
  589. void Hit_Button4(win, gad)
  590.  
  591. struct Window *win;
  592. struct Gadget *gad;
  593.  
  594. {
  595.     printf("Clicked on Button4\n");
  596. }
  597.  
  598.  
  599. /*------------------------------------------------------------------------*/
  600.  
  601. /*/ Hit_Check1()
  602.  *
  603.  * Check1 gadget handler.
  604.  *
  605.  */
  606.  
  607. void Hit_Check1(win, gad)
  608.  
  609. struct Window *win;
  610. struct Gadget *gad;
  611.  
  612. {
  613.     printf("Check1 now ");
  614.     if (gad->Flags & SELECTED)
  615.     printf("on.\n");
  616.     else
  617.     printf("off.\n");
  618. }
  619.  
  620.  
  621. /*------------------------------------------------------------------------*/
  622.  
  623. /*/ Hit_Check2()
  624.  *
  625.  * Check2 gadget handler.
  626.  *
  627.  */
  628.  
  629. void Hit_Check2(win, gad)
  630.  
  631. struct Window *win;
  632. struct Gadget *gad;
  633.  
  634. {
  635.     printf("Check2 now ");
  636.     if (gad->Flags & SELECTED)
  637.     printf("on.\n");
  638.     else
  639.     printf("off.\n");
  640. }
  641.  
  642.  
  643. /*------------------------------------------------------------------------*/
  644.